﻿
using Fiddler;
using FiddlerExUtils.Properties;
using FiddlerExUtils.Tools;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace FiddlerExUtils.Components
{
    public partial class TreeViewComponent : TreeView
    {
        public delegate void ScrollEventHandle(Message m);

        public event ScrollEventHandle Scroll;

        private const int WM_MOUSEWHEEL = 0x020A;
        private const int WM_HSCROLL = 0x0114;
        private const int WM_VSCROLL = 0x0115;

        public Control PairedControl;

        public TreeViewComponent()
        {
            InitializeComponent();
        }

        public TreeViewComponent(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL || m.Msg == WM_MOUSEWHEEL)
            {
                if (Scroll != null)
                    Scroll(m);
            }
            base.WndProc(ref m);
        }

        public void PubWndProc(ref Message m)
        {
            if (m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL)
            {
                int ipos = WindowsApi.GetScrollPos(PairedControl.Handle, 1);
                WindowsApi.SetScrollPos(this.Handle, 1, ipos, true);
            }
            base.WndProc(ref m);
        }

        private void PB_CollapseOrExpandAll_Click(object sender, System.EventArgs e)
        {
            PB_CollapseOrExpandAll.Enabled = false;
            if ((string)PB_CollapseOrExpandAll.Tag == "Collapse")
            {
                if (this.Nodes.Count > 0)
                {
                    this.BeginUpdate();
                    this.CollapseAll();
                    this.Nodes[0].Expand();

                    for (int i = 0; i < this.Nodes[0].Nodes.Count; i++)
                    {
                        this.Nodes[0].Nodes[i].Expand();
                    }
                    if (this.SelectedNode != null)
                    {
                        this.SelectedNode.EnsureVisible();
                    }
                    else
                    {
                        this.Nodes[0].EnsureVisible();
                    }
                    this.EndUpdate();
                    PB_CollapseOrExpandAll.Tag = "Expand";
                    PB_CollapseOrExpandAll.Image = Resources.expand_all;
                    toolTip1.SetToolTip(PB_CollapseOrExpandAll, "全部展开");
                }
            }
            else if ((string)PB_CollapseOrExpandAll.Tag == "Expand")
            {
                if (this.Nodes.Count > 0)
                {
                    this.BeginUpdate();
                    this.ExpandAll();
                    if (this.SelectedNode != null)
                    {
                        this.SelectedNode.EnsureVisible();
                    }
                    else
                    {
                        this.Nodes[0].EnsureVisible();
                    }
                    this.EndUpdate();

                    PB_CollapseOrExpandAll.Tag = "Collapse";
                    PB_CollapseOrExpandAll.Image = Resources.collapse_all;
                    toolTip1.SetToolTip(PB_CollapseOrExpandAll, "全部折叠");
                }
            }
            PB_CollapseOrExpandAll.Enabled = true;
        }

        private void ContextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Point pt = this.PointToClient(Cursor.Position);
            TreeNode nodeAt = this.GetNodeAt(pt);
            if (nodeAt != null)
            {
                this.SelectedNode = nodeAt;
            }
        }

        /// <summary>
        /// 复制目标节点条件的JSON Path到剪贴板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TsmiCopyNodeJsonPath_Click(object sender, EventArgs e)
        {
            if (this.SelectedNode != null)
            {
                Utilities.CopyToClipboard(CommonFunction.GetTreeNodeJSONPath(this.SelectedNode));
            }
        }

        /// <summary>
        /// 复制节点Key+Value值到剪贴板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TsmiCopyNodeKeyAndValue_Click(object sender, EventArgs e)
        {
            if (this.SelectedNode != null)
            {
                Utilities.CopyToClipboard(CommonFunction.GetTreeNodeFullText(this.SelectedNode));
            }
        }

        /// <summary>
        /// Ctrl + C 复制JSON Path到剪贴板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TvJSON_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.C)
            {
                if (this.SelectedNode != null)
                {
                    Utilities.CopyToClipboard(CommonFunction.GetTreeNodeJSONPath(this.SelectedNode));
                }
            }

        }

        /// <summary>
        /// 复制节点Key
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TSMI_CopyNodeKey_Click(object sender, EventArgs e)
        {
            if (this.SelectedNode != null)
            {
                string fullText = CommonFunction.GetTreeNodeFullText(this.SelectedNode);
                if (fullText.Contains("="))
                {
                    Utilities.CopyToClipboard(fullText.Split('=')[0]);
                }
                else
                {
                    Utilities.CopyToClipboard(fullText);
                }

            }
        }

        /// <summary>
        /// 复制节点Value
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TSMI_CopyNodeValue_Click(object sender, EventArgs e)
        {
            if (this.SelectedNode != null)
            {
                string fullText = CommonFunction.GetTreeNodeFullText(this.SelectedNode);
                if (fullText.Contains("="))
                {
                    int index = fullText.IndexOf("=") + 1;
                    Utilities.CopyToClipboard(fullText.Substring(index, fullText.Length - index));
                }
                else
                {
                    Utilities.CopyToClipboard(fullText);
                }
            }
        }

        /// <summary>
        /// 发送节点Key+Value至TextWziard
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TSMI_SendNodeToTextWizard_Click(object sender, EventArgs e)
        {
            if (this.SelectedNode != null)
            {
                FiddlerApplication.UI.actShowTextWizard(CommonFunction.GetTreeNodeFullText(this.SelectedNode));
            }
        }

        /// <summary>
        /// 发送节点Value至Chrome打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TSMI_OpenWithChrome_Click(object sender, EventArgs e)
        {
            if (this.SelectedNode != null)
            {
                string fullText = CommonFunction.GetTreeNodeFullText(this.SelectedNode);
                if (fullText.Contains("="))
                {
                    int index = fullText.IndexOf("=") + 1;
                    string url = fullText.Substring(index, fullText.Length - index).Trim();
                    if (url != "")
                    {
                        System.Diagnostics.Process.Start("chrome.exe", url);
                    }
                }                
            }
        }

    }
}
